﻿using Little.Soldier;
using Little.Soldier.SWIFT.Validation;
using Little.Soldier.SWIFT.Validation.Block1;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Xml;

namespace Little.Soldier.SWIFT
{
  [DesignTimeVisible(false)]
  [ToolboxItem(false)]
  [TypeConverter(typeof(ISwiftBlock1))]
  [Serializable]
  public class SwiftBlock1 : SwiftValueBlock
  {
    private string _ApplicationIdentifier;
    private string _LTAddress;
    private string _SequenceNumber;
    private string _ServiceIdentifier;
    private string _SessionNumber;

    public override string BlockName
    {
      get
      {
        return "1";
      }
    }

    public override int BlockNumber
    {
      get
      {
        return 1;
      }
    }

    public override bool IsEmpty
    {
      get
      {
        switch (this.ApplicationIdentifier)
        {
          case null:
            if (this.ServiceIdentifier == null && (this.LTAddress == null && this.SessionNumber == null))
              return this.SequenceNumber == null;
            else
              break;
        }
        return false;
      }
    }

    public virtual string ApplicationIdentifier
    {
      get
      {
        return this._ApplicationIdentifier;
      }
      set
      {
        this._ApplicationIdentifier = value;
      }
    }

    public virtual string LTAddress
    {
      get
      {
        return this._LTAddress;
      }
      set
      {
        this._LTAddress = value;
      }
    }

    public virtual string SequenceNumber
    {
      get
      {
        return this._SequenceNumber;
      }
      set
      {
        this._SequenceNumber = value;
      }
    }

    public virtual string ServiceIdentifier
    {
      get
      {
        return this._ServiceIdentifier;
      }
      set
      {
        this._ServiceIdentifier = value;
      }
    }

    public virtual string SessionNumber
    {
      get
      {
        return this._SessionNumber;
      }
      set
      {
        this._SessionNumber = value;
      }
    }

    public SwiftBlock1(SwiftMessage swiftMessage)
      : base(swiftMessage)
    {
    }

    public SwiftBlock1(SwiftMessage swiftMessage, string applicationIdentifier, string serviceIdentifier, string lTAddress, string sessionNumber, string sequenceNumber)
      : base(swiftMessage)
    {
      this.ApplicationIdentifier = applicationIdentifier;
      this.ServiceIdentifier = serviceIdentifier;
      this.LTAddress = lTAddress;
      this.SessionNumber = sessionNumber;
      this.SequenceNumber = sequenceNumber;
    }

    public override void InitializeRules()
    {
      this.Rules.Add((IValidate) new Validate_ApplicationIdentifier(typeof (SwiftBlock1).GetProperty("ApplicationIdentifier"), (object) this));
      this.Rules.Add((IValidate) new Validate_ServiceIdentifier(typeof (SwiftBlock1).GetProperty("ServiceIdentifier"), (object) this));
      this.Rules.Add((IValidate) new Validate_LTAddress(typeof (SwiftBlock1).GetProperty("LTAddress"), (object) this));
      this.Rules.Add((IValidate) new Validate_SessionNumber(typeof (SwiftBlock1).GetProperty("SessionNumber"), (object) this));
      this.Rules.Add((IValidate) new Validate_SequenceNumber(typeof (SwiftBlock1).GetProperty("SequenceNumber"), (object) this, true));
    }

    public override bool Equals(object obj)
    {
      if (this != obj)
        goto label_28;
label_27:
      return true;
label_28:
      if (!base.Equals(obj) || this.GetType() != obj.GetType())
        return false;
      SwiftBlock1 swiftBlock1 = (SwiftBlock1) obj;
      if (this.ApplicationIdentifier == null)
      {
        if (swiftBlock1.ApplicationIdentifier != null)
          return false;
      }
      else if (this.ApplicationIdentifier != swiftBlock1.ApplicationIdentifier)
        return false;
      if (this.LTAddress == null)
      {
        if (swiftBlock1.LTAddress != null)
          return false;
      }
      else if (this.LTAddress != swiftBlock1.LTAddress)
        return false;
      if (this.SequenceNumber == null)
      {
        if (swiftBlock1.SequenceNumber != null)
          return false;
      }
      else if (this.SequenceNumber != swiftBlock1.SequenceNumber)
        return false;
      if (this.ServiceIdentifier == null)
      {
        if (swiftBlock1.ServiceIdentifier != null)
          return false;
      }
      else if (this.ServiceIdentifier != swiftBlock1.ServiceIdentifier)
        return false;
      if (this.SessionNumber == null)
      {
        if (swiftBlock1.SessionNumber != null)
          return false;
        else
          goto label_27;
      }
      else if (this.SessionNumber != swiftBlock1.SessionNumber)
        return false;
      else
        goto label_27;
    }

    public override int GetHashCode()
    {
      return 31 * (31 * (31 * (31 * (31 * base.GetHashCode() + (this.ApplicationIdentifier != null ? this.ApplicationIdentifier.GetHashCode() : 0)) + (this.LTAddress != null ? this.LTAddress.GetHashCode() : 0)) + (this.SequenceNumber != null ? this.SequenceNumber.GetHashCode() : 0)) + (this.ServiceIdentifier != null ? this.ServiceIdentifier.GetHashCode() : 0)) + (this.SessionNumber != null ? this.SessionNumber.GetHashCode() : 0);
    }

    internal override XmlNode ToXml(XmlDocument doc)
    {
      XmlElement element1 = doc.CreateElement("BasicHeaderBlock");
      XmlElement element2 = doc.CreateElement("ApplicationIdentifier");
      element2.InnerText = this.ApplicationIdentifier;
      element1.AppendChild((XmlNode) element2);
      XmlElement element3 = doc.CreateElement("ServiceIdentifier");
      element3.InnerText = this.ServiceIdentifier;
      element1.AppendChild((XmlNode) element3);
      XmlElement element4 = doc.CreateElement("LTAddress");
      element4.InnerText = this.LTAddress;
      element1.AppendChild((XmlNode) element4);
      XmlElement element5 = doc.CreateElement("SessionNumber");
      element5.InnerText = this.SessionNumber;
      element1.AppendChild((XmlNode) element5);
      XmlElement element6 = doc.CreateElement("SequenceNumber");
      element6.InnerText = this.SequenceNumber;
      element1.AppendChild((XmlNode) element6);
      return (XmlNode) element1;
    }

    public override string GetValue()
    {
      if (this.IsEmpty)
        return (string) null;
      StringBuilder stringBuilder = new StringBuilder();
      if (this.ApplicationIdentifier != null)
        stringBuilder.Append(this.ApplicationIdentifier);
      if (this.ServiceIdentifier != null)
        stringBuilder.Append(this.ServiceIdentifier);
      if (this.LTAddress != null)
        stringBuilder.Append(this.LTAddress);
      if (this.SessionNumber != null)
        stringBuilder.Append(this.SessionNumber);
      if (this.SequenceNumber != null)
        stringBuilder.Append(this.SequenceNumber);
      return ((object) stringBuilder).ToString();
    }

    public override bool SetValue(string value, List<ValidationError> parseError)
    {
      bool flag = true;
      int length = value.Length;
      switch (length)
      {
        case 25:
        case 27:
          if (length == 15 || length == 17)
            goto default;
          else
            break;
        default:
          if (parseError != null)
          {
            ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, ((object) new StringBuilder().Append("expected a 25 (or 27) or 15 (or 17) chars string and obtained a ").Append(value.Length).Append(" chars string: ").Append(value)).ToString());
            parseError.Add(validationError);
          }
          flag = false;
          break;
      }
      int start1 = 0;
      switch (length)
      {
        case 17:
        case 27:
          if (!value.StartsWith("1:"))
          {
            if (parseError != null)
            {
              ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, ((object) new StringBuilder().Append("expected '1:' at the beginning of Value and found '").Append(value.Substring(0, 1)).Append("'")).ToString());
              parseError.Add(validationError);
            }
            flag = false;
          }
          start1 = 2;
          break;
      }
      int size1 = 1;
      this.ApplicationIdentifier = this.GetValuePart(value, start1, size1);
      int start2 = start1 + size1;
      int size2 = 2;
      this.ServiceIdentifier = this.GetValuePart(value, start2, size2);
      int start3 = start2 + size2;
      int size3 = 12;
      this.LTAddress = this.GetValuePart(value, start3, size3);
      int start4 = start3 + size3;
      int size4 = 4;
      this.SessionNumber = this.GetValuePart(value, start4, size4);
      int start5 = start4 + size4;
      int size5 = 6;
      this.SequenceNumber = this.GetValuePart(value, start5, size5);
      int num = start5 + size5;
      return flag;
    }
  }
}
